package com.campus.controller;

import com.campus.entry.SysUser;
import com.campus.exception.BusinessException;
import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import com.campus.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @PostMapping("/save")
    public ResponseResult<Object> saveSysUser(@RequestBody SysUser sysUser){
        if (sysUser.getPassword()==null || sysUser.getLoginName()==null){
            throw new BusinessException(ResultCode.ERROR);
        }
        sysUserService.saveSysUser(sysUser);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }
    @GetMapping("/getById")
    public ResponseResult<SysUser> getById(@RequestParam Integer userId){
        if (Objects.isNull(userId)){
            throw new BusinessException(ResultCode.ERROR);
        }
        SysUser sysUserById = sysUserService.getSysUserById(userId);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,sysUserById);

    }
    @GetMapping("/getByName")
    public ResponseResult<SysUser> getByName(@RequestParam String loginName){
        if (Objects.isNull(loginName)){
            throw new BusinessException(ResultCode.ERROR);
        }
        SysUser sysUserByName = sysUserService.getSysUserByName(loginName);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,sysUserByName);
    }
}
